package cn.itcast.web.controller.cargo;


import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.service.cargo.ShippingService;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.utils.BeanMapUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.InputStream;
import java.util.*;

@Controller
@RequestMapping("/cargo/shipping")
@Slf4j
public class ShippingController extends BaseController {

    @Reference
    private ShippingService shippingService;
    @Reference
    private ExportService exportServicec;
    @Reference
    private PackingService packingService;

    /**
     * 委托单列表
     * @param pageNum 当前页
     * @param pageSize 页大小
     * @return
     */
    @RequestMapping("/list")
    public String list(
                       @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize){
        //根据企业id查询
        ShippingExample example = new ShippingExample();
        //降序排序
        example.setOrderByClause("create_time desc");
        example.createCriteria();

        //分页
        PageInfo<Shipping> pageInfo = shippingService.findByPage(example, pageNum, pageSize);
        request.setAttribute("pageInfo",pageInfo);
        return "cargo/shipping/shipping-list";
    }


    /**
     * 点击生成委托单，进入委托单添加页面
     * @param id
     * @return
     */
    @RequestMapping("toAdd")
    public String toAdd(String id){
        Shipping shipping = shippingService.PageBean(id);
        //获取正本通知人
        shipping.setNotifyParty(getLoginUser().getUserName());

        Packing packing = packingService.findById(id);

        String exportIds = packing.getExportIds();

        Export export = exportServicec.findById(exportIds);
        request.setAttribute("export",export);
        request.setAttribute("packing",packing);
        request.setAttribute("shipping",shipping);
        request.setAttribute("id",id);
        return "cargo/shipping/shipping-toShipping";
    }
    /**
     * 添加
     * @param shipping
     * @return
     */
    @RequestMapping("edit")
    public String edit(Shipping shipping){

        // 设置创建时间、创建人、创建人的部门
        shipping.setCreateTime(new Date());
        shipping.setCreateBy(getLoginUser().getId());
        shipping.setCreateDept(getLoginUser().getDeptId());
        //保存
        shippingService.save(shipping);
        return "redirect:/cargo/shipping/list";
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public String delete(String id) {
        shippingService.delete(id);
        return "redirect:/cargo/shipping/list";
    }

    /**
     * 查看
     */
    @RequestMapping("/view")
    public String view(String id){
        Shipping shipping = shippingService.findById(id);
        request.setAttribute("shipping",shipping);
        return "cargo/shipping/shipping-view";
    }


}
